package com.cloudwise.io;

import cn.hutool.core.io.watch.Watcher;
import org.springframework.util.StopWatch;

import java.io.*;

/**
 * @author ：Teacher陈
 * @date ：Created in 2020/12/29 10:15
 * @description：测试javaIo流
 * @modified By：
 * @version: 1.0
 */
public class TestStream {


    /**
     * @create by: Teacher陈
     * @description: 文件拷贝
     * @create time: 2020/12/29 11:04
     * @param resource  dest
     * @return
     */
    public  static  void  fileCopy(String  resource,String dest) throws Exception {
        //获取源文件对象
        File resFile = new File(resource);
        //获取目标文件对象
        File destFile= new File(dest);
        //创建一个文件输入流
        FileInputStream  fileInputStream= new FileInputStream(resFile);
        BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
        //创建一个文件输出流
        FileOutputStream fileOutputStream= new FileOutputStream(destFile);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        byte [] bytes= new byte[1024];
        //循环读取文件内容
        int count=0;
       while ( ( count = bufferedInputStream.read(bytes))!=-1){
           //循环写入文件内容
           bufferedOutputStream.write(bytes,0,count);
       }
       //刷新缓冲区
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        bufferedInputStream.close();
        fileOutputStream.close();
       fileInputStream.close();


    }
    /**
     * @create by: Teacher陈
     * @description: 字符流文件拷贝
     * @create time: 2020/12/29 11:04
     * @param resource  dest
     * @return
     */
    public  static  void  fileCopyChar(String  resource,String dest) throws Exception {
        //获取源文件对象
        File resFile = new File(resource);
        //获取目标文件对象
        File destFile= new File(dest);
        //创建一个文件输入流
        //FileInputStream  fileInputStream= new FileInputStream(resFile);
        Reader reader= new FileReader(resFile);
       // BufferedInputStream bufferedInputStream= new BufferedInputStream(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        //创建一个文件输出流
       // FileOutputStream fileOutputStream= new FileOutputStream(destFile);
        Writer writer= new FileWriter(destFile) ;
       // BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        BufferedWriter  bufferedWriter = new BufferedWriter(writer);
        char [] bytes= new char[1024];
        //循环读取文件内容
        int count=0;
        while ( ( count = bufferedReader.read(bytes))!=-1){
            //循环写入文件内容
            bufferedWriter.write(bytes,0,count);
        }
        //刷新缓冲区
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();
        writer.close();
        reader.close();


    }
    public static void main(String[] args) throws Exception {
      /*  //获取文件对象
        File file = new File("d:\\aaa.txt");
        //创建一个文件输入流
        FileInputStream  fileInputStream= new FileInputStream(file);
        byte [] bytes= new byte[4];
        //读取的字符个数
        int count = fileInputStream.read(bytes);
        if(count!=-1){
            System.out.println("读取的内容："+new String(bytes,0,count)+"读取的字符个数"+count);
        }

        count = fileInputStream.read(bytes);
        if(count!=-1){
            System.out.println("读取的内容："+new String(bytes,0,count)+"读取的字符个数"+count);
        }
        count = fileInputStream.read(bytes);
        if(count!=-1){
            System.out.println("读取的内容："+new String(bytes,0,count)+"读取的字符个数"+count);
        }
        count = fileInputStream.read(bytes);
        if(count!=-1){
            System.out.println("读取的内容："+new String(bytes,0,count)+"读取的字符个数"+count);
        }
        count = fileInputStream.read(bytes);
        if(count!=-1){
            System.out.println("读取的内容："+new String(bytes,0,count)+"读取的字符个数"+count);
        }*/
        StopWatch stopWatch= new StopWatch();
        stopWatch.start();
        //TestStream.fileCopy("F:\\software\\oracle.zip","d:\\aaaa.zip");
        TestStream.fileCopyChar("d:\\aaa.txt","f:\\aaaa.txt");
        stopWatch.stop();
        long totalTimeMillis = stopWatch.getTotalTimeMillis();
        System.out.println(totalTimeMillis);
    }



}
